/**
 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or http://ckcom/license
 */
CKEDITOR.tools.enableHtml5Elements(document);
CKEDITOR.editorConfig = function (config) {
    // Define changes to default configuration here. For example:
    config.language = 'zh-cn';
    config.uiColor = '#ffffff';
    config.toolbar = [
        {name: 'basicstyles', items: ['Bold', 'Italic', 'Strike']},
        {name: 'colors', items: ['TextColor', 'BGColor', "Format", "FontSize"]},
        {name: 'insert', items: ["Image","Video", "CodeSnippet", "HorizontalRule"]},
        {name: 'links', items: ['Outdent', 'Indent', 'Link']},
        {name: 'paragraph', items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock']},
        {name: 'tools', items: ['Maximize', 'Source']}
    ];
    config.removeButtons = 'Underline,Subscript,Superscript';
    config.format_tags = 'h1;h2;h3;h4;p';
    config.extraPlugins = 'maximize,codesnippet,image2,video,uploadwidget,uploadimage';
    config.removeDialogTabs = 'image:advanced;image:Link;link:advanced';
    config.codeSnippet_theme = 'monokai';

    config.filebrowserUploadUrl = '/upload';
    config.uploadUrl = '/upload?type=uploadfile';
    config.toolbarLocation = 'top';
    config.allowedContent = true;
    config.extraAllowedContent = 'video [*]{*};source [*]{*}(*);';

    config.title = false;
    config.height = 'auto';
    // Rails CSRF token
    config.filebrowserParams = function () {
        var csrf_token, csrf_param, meta,
            metas = document.getElementsByTagName('meta'),
            params = new Object();

        for (var i = 0; i < metas.length; i++) {
            meta = metas[i];

            switch (meta.name) {
                case "csrf-token":
                    csrf_token = meta.content;
                    break;
                case "csrf-param":
                    csrf_param = meta.content;
                    break;
                default:
                    continue;
            }
        }

        if (csrf_param !== undefined && csrf_token !== undefined) {
            params[csrf_param] = csrf_token;
        }
        params["_xsrf"] = getCookieByName("_xsrf");
        return params;
    };

    config.addQueryString = function (url, params) {
        var queryString = [];

        if (!params) {
            return url;
        } else {
            for (var i in params)
                queryString.push(i + "=" + encodeURIComponent(params[i]));
        }

        return url + ( ( url.indexOf("?") != -1 ) ? "&" : "?" ) + queryString.join("&");
    };
    // Integrate Rails CSRF token into file upload dialogs (link, image, attachment and flash)
    CKEDITOR.on('dialogDefinition', function (ev) {
        // Take the dialog name and its definition from the event data.
        var dialogName = ev.data.name;
        var dialogDefinition = ev.data.definition;
        var content, upload;

        if (CKEDITOR.tools.indexOf(['link', 'image', 'attachment', 'flash'], dialogName) > -1) {
            content = (dialogDefinition.getContents('Upload') || dialogDefinition.getContents('upload'));
            upload = (content == null ? null : content.get('upload'));

            if (upload && upload.filebrowser && upload.filebrowser['params'] === undefined) {
                upload.filebrowser['params'] = config.filebrowserParams();
                upload.action = config.addQueryString(upload.action, upload.filebrowser['params']);
            }
        }

        if (dialogName == 'link') {
            // Get a reference to the "Target" tab.
            var targetTab = dialogDefinition.getContents('target');

            // Set the default value for the target field.
            var targetField = targetTab.get('linkTargetType');
            targetField['default'] = '_blank';
        }

    });
    //设置快捷键
    config.keystrokes = [
        [CKEDITOR.CTRL + 77 /*M*/, 'maximize'],  //文本菜单
        [CKEDITOR.CTRL + 83 /*M*/, 'save'],  //保存

    ]

};
